package com.tianji.learning.service.impl;

import com.tianji.common.exceptions.BadRequestException;
import com.tianji.common.utils.BeanUtils;
import com.tianji.learning.domain.po.PointsBoardSeason;
import com.tianji.learning.domain.vo.PointsBoardSeasonVO;
import com.tianji.learning.mapper.PointsBoardSeasonMapper;
import com.tianji.learning.service.IPointsBoardSeasonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LYQ
 * @since 2024-07-01
 */
@Service
public class PointsBoardSeasonServiceImpl extends ServiceImpl<PointsBoardSeasonMapper, PointsBoardSeason> implements IPointsBoardSeasonService {

    /**
     * 查询历史赛季列表
     *
     * @return
     */
    @Override
    public List<PointsBoardSeasonVO> queryHistorySeason() {
        List<PointsBoardSeason> result = lambdaQuery()
                .le(PointsBoardSeason::getEndTime, LocalDateTime.now().plusMonths(1))
                .list();
        return BeanUtils.copyToList(result, PointsBoardSeasonVO.class);
    }

    /**
     * 根据日期查询赛季id
     *
     * @param date
     * @return
     */
    @Override
    public Integer querySeasonIdByDate(LocalDateTime date) {
        PointsBoardSeason season = lambdaQuery()
                .le(PointsBoardSeason::getBeginTime, date)
                .ge(PointsBoardSeason::getEndTime, date)
                .one();
        if (season == null){
            throw new BadRequestException("赛季不存在");
        }
        return season.getId();
    }
}
